function t(t, e) {
    setTimeout(function() {
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 400 * t.deviceRatio,
            height: 400 * t.deviceRatio,
            canvasId: "tempCanvas",
            success: function(e) {
                wx.hideLoading(), console.log(e.tempFilePath), t.setData({
                    tempImageSrc: e.tempFilePath
                });
            }
        });
    }, e);
}

function e(t, o, i, a) {
    return t > i ? e(i, o / (t / i), i, a) : o > a ? e(t / (o / a), a, i, a) : t <= i && o <= a ? {
        width: t,
        height: o
    } : e(t, t, i, a);
}

var o = getApp();

Page({
    data: {
        emotionUrl: "",
        allText: [],
        isTextEdit: !1,
        isChooseFontSize: !1,
        isChooseFontColor: !1,
        isChooseFontPattern: !1,
        inputFocus: !1,
        texted: !1,
        allColor: [ "#7f7f7f", "#000000", "#880015", "#ed1c24", "#ff7f27", "#fff200", "#00a2e8", "#22b14c", "#ffaec9", "#a349a4", "#ffffff", "#c3c3c3" ],
        tempImageSrc: "",
        ad: {}
    },
    onShow(){
        this.getRandomAd()
        getApp().chaping()
    },
    onLoad: function(t) {
        getApp().getRandomAd(true,this)
        this.getRandomAd(true)
        this.device = o.globalData.myDevice, this.deviceRatio = this.device.windowWidth / 750, 
        this.setData({
            emotionUrl: t.choosedEmoUrl
        });
    },
    chooseaddText: function() {
        this.getRandomAd()
        var t = this.data.allText;
        t.push({
            idx: t.length - 1,
            someText: "点击输入文字",
            fontColor: this.fontColor ? this.fontColor : "rgba(20,20,20,1)",
            fontSize: this.fontSize ? this.fontSize : 14,
            fontStyle: "normal",
            fontWeight: "normal",
            textL: 285 * this.deviceRatio,
            textT: 420 * this.deviceRatio,
            isTextActive: !0
        }), this.setData({
            isTextEdit: !0,
            allText: t,
            isChooseFontSize: !1,
            isChooseFontColor: !1,
            isChooseFontPattern: !1
        });
    },
    onShareAppMessage: function() {
        return {
            title: "表情包制作-图片编辑助手",
            path: "pages/emotionChoose/emotionChoose"
        };
    },
    focusInput: function() {
        this.setData({
            inputFocus: !this.data.inputFocus
        });
    },
    inputText: function(t) {
        var e = this.data.allText;
        e[e.length - 1].someText = t.detail.value, 0 == e[e.length - 1].someText.length && (e[e.length - 1].someText = "点击输入文字"), 
        this.setData({
            allText: e
        });
    },
    textMoveStart: function(t) {
        this.textX = t.touches[0].clientX, this.textY = t.touches[0].clientY;
    },
    textMove: function(t) {
        var e = this.data.allText, o = t.touches[0].clientX - this.textX, i = t.touches[0].clientY - this.textY, a = 750 * this.deviceRatio - 50, s = 1e3 * this.deviceRatio - 50, n = e[e.length - 1].textL + o, h = e[e.length - 1].textT + i;
        n < 0 && (n = 0), n > a && (n = a), h < 0 && (h = 0), h > s && (h = s), e[e.length - 1].textL = n, 
        e[e.length - 1].textT = h, this.setData({
            allText: e,
            isChooseFontSize: !1,
            isChooseFontColor: !1,
            isChooseFontPattern: !1
        }), this.textX = t.touches[0].clientX, this.textY = t.touches[0].clientY;
    },
    chooseFontsize: function() {
        this.getRandomAd()
        this.setData({
            isChooseFontSize: !this.data.isChooseFontSize,
            isChooseFontColor: !1,
            isChooseFontPattern: !1
        });
    },
    fontsizeSliderChange: function(t) {
        this.fontSize = t.detail.value;
        var e = this.data.allText;
        e[e.length - 1] && e[e.length - 1].isTextActive && (e[e.length - 1].fontSize = this.fontSize, 
        this.setData({
            allText: e
        }));
    },
    chooseFontColor: function() {
        this.getRandomAd()
        this.setData({
            isChooseFontSize: !1,
            isChooseFontColor: !this.data.isChooseFontColor,
            isChooseFontPattern: !1
        });
    },
    fontColorChange: function(t) {
        this.fontColor = t.target.dataset.selected;
        var e = this.data.allText;
        e[e.length - 1] && e[e.length - 1].isTextActive && (e[e.length - 1].fontColor = this.fontColor, 
        this.setData({
            allText: e
        }));
    },
    chooseFontPattern: function() {
        this.getRandomAd()
        this.setData({
            isChooseFontSize: !1,
            isChooseFontColor: !1,
            isChooseFontPattern: !this.data.isChooseFontPattern
        });
    },
    fontStyleChange: function(t) {
        this.fontStyle = t.detail.value ? "oblique" : "normal";
        var e = this.data.allText;
        e[e.length - 1] && e[e.length - 1].isTextActive && (e[e.length - 1].fontStyle = this.fontStyle, 
        this.setData({
            allText: e
        }));
    },
    fontWeightChange: function(t) {
        this.fontWeight = t.detail.value ? "bold" : "normal";
        var e = this.data.allText;
        e[e.length - 1] && e[e.length - 1].isTextActive && (e[e.length - 1].fontWeight = this.fontWeight, 
        this.setData({
            allText: e
        }));
    },
    cancelAddText: function() {
        var t = this.data.allText;
        t.pop(), this.setData({
            allText: t,
            isTextEdit: !1,
            inputFocus: !1,
            isChooseFontSize: !1,
            isChooseFontColor: !1,
            isChooseFontPattern: !1
        });
    },
    competeAddText: function() {
        var o = this, i = this.data.allText;
        if ("点击输入文字" == i[i.length - 1].someText || "" == i[i.length - 1].someText) this.cancelAddText(); else {
            i[i.length - 1].isTextActive = !1, this.setData({
                allText: i,
                isTextEdit: !1,
                inputFocus: !1,
                isChooseFontSize: !1,
                isChooseFontColor: !1,
                isChooseFontPattern: !1,
                texted: !0
            });
            var a = wx.createCanvasContext("tempCanvas");
            a.fillStyle = "#fff", a.fillRect(0, 0, 400 * o.deviceRatio, 400 * o.deviceRatio), 
            wx.getImageInfo({
                src: o.data.tempImageSrc ? o.data.tempImageSrc : o.data.emotionUrl,
                success: function(s) {
                    var n = e(s.width, s.height, 400 * o.deviceRatio, 400 * o.deviceRatio), h = (400 * o.deviceRatio - n.width) / 2;
                    a.drawImage(o.data.tempImageSrc ? o.data.tempImageSrc : o.data.emotionUrl, h, 0, n.width, n.height), 
                    a.setFillStyle(i[i.length - 1].fontColor), a.font = i[i.length - 1].fontStyle + " " + i[i.length - 1].fontWeight + " " + i[i.length - 1].fontSize + "px sans-serif", 
                    a.setTextAlign("left"), a.setTextBaseline("top"), a.fillText(i[i.length - 1].someText, i[i.length - 1].textL - 170 * o.deviceRatio, i[i.length - 1].textT - 90 * o.deviceRatio), 
                    a.draw(!0), t(o, 200);
                }
            });
        }
    },
    saveImgToPhone: function() {
        wx.navigateTo({
            url: "/pages/preview/preview?imageurl=" + (this.data.tempImageSrc ? this.data.tempImageSrc : this.data.emotionUrl)
        });
    }
});